//
//  LFPlaceHolderWaveView.m
//  AntFarms
//
//  Created by LF on 2017/7/21.
//  Copyright © 2017年 MuXi. All rights reserved.
//

#import "LFPlaceHolderWaveView.h"

@interface LFPlaceHolderWaveView ()
{
    //基础变量
    CGFloat waveA;//水纹的振幅
    CGFloat waveW;//水纹的周期
    CGFloat offestX;//位移
    CGFloat currentK;//当前波纹的高度
    CGFloat waveSpeed;//当前水纹的速度
    CGFloat waveSpeed2;
    CGFloat waveWidth;//水纹宽度
    //基础变量
    CGFloat waveA2;//水纹的振幅
    CGFloat waveW2;//水纹的周期
    CGFloat offestX2;//位移
    CGFloat currentK2;//当前波纹的高度
}
/**
 CADisplayLink的对象也是一个定时器。适用于UI的不停刷新，如自定义动画引擎或者视频的渲染。
 */
@property(nonatomic,strong)CADisplayLink * waveDispalyLink;

@property(nonatomic,strong)CAShapeLayer * waveShaperLayer;

@property(nonatomic,strong)CAShapeLayer * waveShaperLayer2;

@end

@implementation LFPlaceHolderWaveView

- (instancetype) initWithFrame:(CGRect)frame {
    
    self = [super initWithFrame:frame];
    if (!self) {
        return nil;
    }

    [self initConfig];
    
    return self;
}

- (instancetype) init {
    
    self = [super init];
    if (!self) {
        return nil;
    }
    
    [self initConfig];
    
    return self;
}

- (void) initConfig {
    
    self.backgroundColor = [UIColor whiteColor];
    
    self.clipsToBounds = YES;
    
    //设置波浪的宽度
    waveWidth = [UIScreen mainScreen].bounds.size.width;
    
    //波浪的速度
    waveSpeed = 0.03;
//    waveSpeed2 = 0.1;
    //设置波浪流动速度
    
    //设置振幅
    waveA = 10;
    //设置周期
    waveW = 1/30.0;
    //设置波浪纵向位置
    currentK = kHalfHeight;
    
//    //设置振幅
//    waveA2 = 10;
//    //设置周期
//    waveW2 = 1/30.0;
//    //设置波浪纵向位置
//    currentK2 = kHalfHeight/2;
    
    //第一条波浪线
    _waveShaperLayer = [CAShapeLayer layer];
    _waveShaperLayer.strokeStart = 0.0;
    _waveShaperLayer.strokeEnd = 0.6;
    //闭环颜色
//    _waveShaperLayer.fillColor = [[UIColor cyanColor] colorWithAlphaComponent:0.3].CGColor;
    _waveShaperLayer.fillColor = UIColorFromRGB(0xff72a1).CGColor;
    //波浪宽度
    _waveShaperLayer.lineWidth = 0.1;
    //边缘颜色
//    _waveShaperLayer.strokeColor = [[UIColor blueColor] colorWithAlphaComponent:0.3].CGColor;
    _waveShaperLayer.strokeColor = UIColorFromRGB(0xff72a1).CGColor;
    [self.layer addSublayer:_waveShaperLayer];
    
    //启动定时器
    _waveDispalyLink = [CADisplayLink displayLinkWithTarget:self selector:@selector(getCurrentWave:)];
    [_waveDispalyLink addToRunLoop:[NSRunLoop mainRunLoop] forMode:NSRunLoopCommonModes];
    
    //第二条波浪线
    //第一条波浪线
    //    _waveShaperLayer2 = [CAShapeLayer layer];
    //    _waveShaperLayer2.strokeStart = 0.0;
    //    _waveShaperLayer2.strokeEnd = 0.8;
    //    //闭环颜色
    //    _waveShaperLayer2.fillColor = [[UIColor purpleColor] colorWithAlphaComponent:0.3].CGColor;
    //    //波浪宽度
    //    _waveShaperLayer2.lineWidth = 0.1;
    //    //边缘颜色
    //    _waveShaperLayer2.strokeColor = [[UIColor blueColor] colorWithAlphaComponent:0.3].CGColor;
    //
    //    [self.view.layer addSublayer:_waveShaperLayer2];
    
    //启动定时器
//    _waveDispalyLink = [CADisplayLink displayLinkWithTarget:self selector:@selector(getCurrentWave2:)];
//    [_waveDispalyLink addToRunLoop:[NSRunLoop mainRunLoop] forMode:NSRunLoopCommonModes];
}

-(void)getCurrentWave:(CADisplayLink *)displayLink
{
    //实时的位移
    offestX += waveSpeed;
    [self createWavePath];
}
//-(void)getCurrentWave2:(CADisplayLink *)displayLink
//{
//    //实时的位移
//    offestX2 += waveSpeed2;
//    [self createWavePath2];
//}
-(void)createWavePath{
    //创建一个路径
    CGMutablePathRef path = CGPathCreateMutable();
    
    CGFloat Y = currentK;
    
    //将点移动到x=0,y=currentK的位置
    CGPathMoveToPoint(path, nil, 0, Y);
    for (NSInteger x = 0.0f; x<=waveWidth; x++) {
        //正玄波浪公式
        Y = waveA * sin(waveW*x+offestX+40)+currentK;
        //将点连成线
        CGPathAddLineToPoint(path, nil, x, Y);
    }
    CGPathAddLineToPoint(path, nil, waveWidth, kHalfHeight * 2);
    CGPathAddLineToPoint(path, nil, 0, kHalfHeight * 2);
    CGPathCloseSubpath(path);
    
    _waveShaperLayer.path = path;
    
    CGPathRelease(path);
    
}
//-(void)createWavePath2{
//    //创建一个路径
//    CGMutablePathRef path = CGPathCreateMutable();
//    
//    CGFloat Y = currentK2;
//    
//    //将点移动到x=0,y=currentK的位置
//    CGPathMoveToPoint(path, nil, 0, Y);
//    for (NSInteger x = 0.0f; x<=waveWidth; x++) {
//        //正玄波浪公式
//        Y = waveA2 * sin(waveW2*x+offestX2)+currentK2;
//        //将点连成线
//        CGPathAddLineToPoint(path, nil, x, Y);
//    }
//    CGPathAddLineToPoint(path, nil, waveWidth, kHalfHeight);
//    CGPathAddLineToPoint(path, nil, 0, kHalfHeight);
//    CGPathCloseSubpath(path);
//    
//    _waveShaperLayer2.path = path;
//    
//    CGPathRelease(path);
//    
//}
-(void)dealloc
{
    [_waveDispalyLink invalidate];
}

@end
